package tuvienna.jade.participant;

import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import tuvienna.jade.Constants;

/**
 * Handling the deregistration of a participant from a table
 */

public class DeregisterAtTableBehaviour extends RegisterAtTableBehaviour {

	private static final long serialVersionUID = 1809253933702899177L;
	private Behaviour nextBehaviour = null;
	private boolean deregistered = false;
	public DeregisterAtTableBehaviour(ParticipantAgent participant, AID table) {
		super(participant, table);
	}

	public DeregisterAtTableBehaviour(ParticipantAgent participant, AID table, Behaviour nextBehaviour) {
		super(participant, table);
		this.nextBehaviour = nextBehaviour;
	}

	public void action() {
		super.action();
		if (done() && deregistered && nextBehaviour != null) {
			participant.addBehaviour(nextBehaviour); 
		}
	}
	
	protected String getConversationID() {
		return Constants.DEREGISTRATION_REQUEST;
	}
	
	protected void success(ACLMessage reply) {
		participant.setTable(null);	// remove table information
		deregistered = true;
	}
	
	protected void failure(ACLMessage reply) {
	}
	
}